package ysq.xyg.host.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.model.po.HostReport;
import ysq.xyg.host.model.vo.HostReportVo;
import ysq.xyg.host.service.HostReportService;

import java.util.Date;
import java.util.List;

public class HostReportJob implements Job {

    private static Logger logger = LoggerFactory.getLogger("api");

    @Autowired
    private HostReportService hostReportService;

    /**
     * 超过15天将客户置为无效
     * 每天晚上11点50执行
     * cron = "0/5 * * * * ? "
     * cron = "0 50 23 1/1 * ? "
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        // 定时任务，超过15天将客户置为无效
        // 处理中的暂时不处理
        logger.info("超过15天将客户置为无效");
        HostReportVo hostReportVo = new HostReportVo();
        hostReportVo.setReportState(BaseConstant.HOST_REPORT_STATE_ONE);
        List<HostReport> hostReports = hostReportService.acceptList(hostReportVo);
        if (CollUtil.isNotEmpty(hostReports)) {
            for (HostReport hostReport : hostReports) {
                // 超过15天
                Date date = DateUtil.offsetDay(hostReport.getCreateTime(), 15);
                if (date.getTime() < System.currentTimeMillis()) {
                    // 无效
                    hostReport.setReportState(BaseConstant.HOST_REPORT_STATE_FOUR);
                    hostReportService.updateByPrimaryKey(hostReport);
                }
            }
        }
    }
}
